package com.ljs.controller;

import com.ljs.entity.Admin;
import com.ljs.entity.Menu;
import com.ljs.entity.Order;
import com.ljs.entity.User;
import com.ljs.feign.AccountFeign;
import com.ljs.feign.MenuFeign;
import com.ljs.feign.OrderFegin;
import com.ljs.feign.UserFeign;
import com.ljs.util.JsonResult;

import com.sun.org.apache.regexp.internal.RE;
import org.apache.catalina.Session;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpSession;
import java.util.LinkedHashMap;
import java.util.List;

@Controller
public class ClientHaddler {
    @Resource
    MenuFeign menuFeign;
    @Resource
    UserFeign userFeign;
    @Resource
    AccountFeign accountFeign;
    @Resource
    OrderFegin orderFegin;

    @GetMapping("/")
    public String index() {
        return "index"; /*直接到首页*/

    }


    @RequestMapping("/client/insert")
    public ModelAndView addmenu(Menu menu) {
        System.out.println("访问成功");
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("要添加的菜是" + menu);


        menuFeign.save(menu); //通过fegin接口来访问menuprovider,再进行sql语句修改
        if (menuFeign.save(menu)) {
            modelAndView.setViewName("index"); //添加成功后返回主页面
        }
        return modelAndView;
    }

    @RequestMapping("/client/findall")
    @ResponseBody
    public JsonResult sel(@RequestParam("page") int page, @RequestParam("limit") int limit) {
        int index = (page - 1) * limit;//layui会传过一个page，limit参数
        List<Menu> menuList = menuFeign.selAll(index, limit);
        JsonResult jsonResult = new JsonResult();
        int count = menuFeign.count();//将数量传过去
        jsonResult.setData(menuList);
        jsonResult.setCount(count);
        return jsonResult;
    }
    @RequestMapping("/client/menu/name/{na}")
    @ResponseBody
    public List<Menu> selbyname(@PathVariable("na") String name) {
        return menuFeign.selbyname(name);
    }

    @GetMapping("/client/index")
    @ResponseBody
    public String menu() {
        return "menu的端口是" + menuFeign.selport();
    }

    @ResponseBody
    @GetMapping("/client/all")
    public JsonResult all() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(menuFeign.all());
        return jsonResult;
    }

    @GetMapping("/client/count")
    @ResponseBody
    public JsonResult count() {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(menuFeign.count());
        return jsonResult;
    }

    @GetMapping("/client/delete/{id}")
    public String delete(@PathVariable("id") int id) {
        menuFeign.delete(id);
        return "index";
    }

    @RequestMapping("/client/add")//跳到添加页面
    public String add() {
        return "addMenu";
    }


    @RequestMapping("/client/upd/{id}")//跳到添加页面
    public String upd(@PathVariable("id") int id, Model model) {
        Menu menu = menuFeign.selbyid(id);
        System.out.println("要修改的是" + menu);
        if (menu != null) {
            model.addAttribute("menu", menu);
        }

        return "updateMenu";
    }

    @RequestMapping("/client/update")
    public ModelAndView update(Menu menu) {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println("修改后的" + menu);
        modelAndView.addObject(menu);
        if (menuFeign.update(menu)) {//修改成功才跳转
            modelAndView.setViewName("index");
            System.out.println("修改成功");
        }

        return modelAndView;
    }

    //查找用户
    @RequestMapping("/client/user/findall")
    @ResponseBody
    public JsonResult seluser(@RequestParam("page") int page, @RequestParam("limit") int limit) {
        int index = (page - 1) * limit;//layui会传过一个page，limit参数
        List<User> menuList = userFeign.selAll(index, limit);
        System.out.println("用户有" + menuList);
        JsonResult jsonResult = new JsonResult();
        int count = userFeign.count();//将数量传过去
        jsonResult.setData(menuList);
        jsonResult.setCount(count);
        return jsonResult;
    }

    @RequestMapping("/client/user") //去用户列表界面
    public String touser() {
        return "User";
    }

    @RequestMapping("/tologin")
    public String toulogin() {
        return "login";
    }

    //登录
    @RequestMapping("/client/login")
    public String login(@RequestParam("username") String username, @RequestParam("password") String password, @RequestParam("type") String type, HttpSession session) {
        Object account=     accountFeign.login(username, password, type);;
              //传过来的这个值不是object,而是hashmap类型
        LinkedHashMap <String,Object> linkedHashMap=(LinkedHashMap)account;
        if (account != null) {

            System.out.println("登录成功的用户" + account);
            if (type.equals("user")) { User user = new User();//强制转化类型
               int id= (int) linkedHashMap.get("id");
                String username1= (String) linkedHashMap.get("username");
                String password1= (String) linkedHashMap.get("password");
                User user1=userFeign.seluserbyid(id);
                System.out.println("user是"+user1);
                session.setAttribute("user", user1);

            } else if (type.equals("admin")) {
             Admin user = new Admin();//强制转化类型
                int id= (int) linkedHashMap.get("id");
                String username1= (String) linkedHashMap.get("username");
                String password1= (String) linkedHashMap.get("password");
                user.setId(id);
                user.setUsername(username1);
                user.setPassword(password1);

                session.setAttribute("user", user);

            }
            return "index"; //登录成功到主页面

        } else {
            return "login";//登录失败，重新登录
        }


    }
    //退出登录
   /* @RequestMapping("/client/loginout")
    public  String loginout(HttpSession session){
        *//*session.removeAttribute("user");*//*
        session.invalidate();
        return "login";
    }*/
    @RequestMapping("/client/order/{mid}")
    public  String  insert(Order order,@PathVariable("mid") int mid,   HttpSession session){
      User user=(User)  session.getAttribute("user");
        System.out.println("当前用户"+user);
        Menu menu=menuFeign .selbyid(mid);
        //这里要根据id来查询菜单才行，不然数据不对
        order.setMenu(menu);
        order.setUser(user);
        System.out.println("添加的订单"+order);
        orderFegin.insert(order);
        return "order";

    }
    @RequestMapping("/client/order/findall")
    @ResponseBody
    public JsonResult selorder(@RequestParam("page") int page,@RequestParam("limit") int limit,HttpSession session){
      //查询全部的订单
        JsonResult jsonResult=new JsonResult();
        User user=(User)  session.getAttribute("user");
      int  index=(page-1)*1;
  int count=orderFegin.count(user.getId());
    List<Order>  orderList=  orderFegin.selOrder(index,limit,user.getId());
        jsonResult.setData(orderList);
        jsonResult.setCount(count);
        return jsonResult;//返回hjson格式的数据
    }
@RequestMapping("/client/toOrder")
    public String tologin(){
        return "order";
}
}


